// 原材料
import React, { Component } from 'react'
import {fetchData} from '../../../components/AjaxConsumer'
import {ListBox} from '../../../components'
import Actions from '../ActionStore/Actions';
import UpdateSerialrulePage from '../Update'
import EditPage from '../EditPage'
import { Modal } from 'antd';

export default class UpListPage extends Component {
    constructor(props) {
        super(props);
        //行操作列表
        this.rowOperation = [
            {
                handleName: 'doubleClick',
                showName: '行双击事件'
            }
        ];
        //header 操作列表
        this.headerOperation = [

            {
                handleName: 'addSerialrule',
                showName: '新增',
                iconClass: 'plus'
            },{
                handleName: 'edit',
                showName: '修改',
                iconClass: 'edit'
            },{
                handleName: 'delete',
                showName: '删除',
                iconClass: 'delete'
            },{
                handleName: 'mixi',
                showName: '明细',
                iconClass: 'file-text'
            }
        ];

        //table列表展示列配置 没用的删掉了
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        this.tableColumn = {
            storageKey: 'ListInstructionColumnConfig',
            data: [
                {
                    showName: 'ruleId',
                    columnName: 'ruleId',
                    columnHidden:true,
                    foreverHidden: true
                },{
                    showName: '规则代码',
                    columnName: 'ruleCode'
                },{
                    showName: '规则描述',
                    columnName: 'ruleDesc'
                },{
                    showName: '长度上限',
                    columnName: 'ruleLength'
                }
            ]
        };

        //搜索属性配置，如果字段不配置，默认是input搜索
        // 搜索类型：
        //      1、输入框 type='input'， 默认
        //      2、时间 type='date'
        //      3、数字 type='number'
        //      4、下拉框 type='select' dicCode dicName
        this.searchComponentData = [
            {
                showName: '规则代码',
                name:'ruleCode',
                type: 'input',
                url: null
            },{
                showName: '规则描述',
                name:'ruleDesc',
                type: 'input',
                url: null
            },{
                showName: '长度上限',
                name:'ruleLength',
                type: 'input',
                url: null
            }
        ];

    }

    rowHandles = (strHandles, rowObj) => {
        if(strHandles == 'doubleClick'){//双击、单击 事件是一样的
            this.rowClickHandle(rowObj);
        }else if(strHandles == 'rowClick'){//双击、单击 事件是一样的
            this.rowClickHandle(rowObj);
        }else if(strHandles === 'edit'){
            this.editHandle(rowObj);
        }else if(strHandles === 'delete'){
            this.deleteHandle(rowObj);
        }else if(strHandles === 'addSerialrule'){
            this.addSerialruleHandle();
        }else if(strHandles === 'mixi'){
            this.editMixiHandle(rowObj);
        }
    }

    // 增加用户
    addSerialruleHandle = () => {
        this.refs.updateSerialrule.addSerialruleHandle();
    }
    editHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else {
            this.refs.updateSerialrule.getFormData[1].disable = true;
            this.refs.updateSerialrule.editHandle(rowObj[0].data);
        }

    }

    /**/
    editMixiHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else {
            const options= {
                url: '/base/data/serialrule/r/detail/'+rowObj[0].data.ruleCode,
                type:'GET',
                condition:{},
            };
            fetchData(this, options, (resData)=>{
                let tempArr = [];
                let tempObj = {};


                tempObj.ruleCode = resData.data.ruleCode

                tempArr.push(tempObj);


                this.refs.editPage.editHandle(resData,rowObj[0].data.ruleCode);
            });

            /// this.refs.updateSerialrule.getFormData[1].disable = true;
           /// this.refs.updateSerialrule.editHandle(rowObj[0].data);
        }

    }


    deleteHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else {
            this.refs.updateSerialrule.deleteHandle(rowObj[0].data)
        }
    }



    rowClickHandle = (rowObj) => {
        Actions.rowClickHandle(rowObj);
    }
    //刷新列表  aaa
    refreshHandle = () => {
        this.refs.listInstruction.refreshHandle();
    }

    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return(
            <div>
                <ListBox
                    ref='listInstruction'
                    isHideLeft={isHideLeft}
                    title=" 序列号规则"
                    tableColumnData = {this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url = '/base/data/serialrule/list'
                    rowHandles = {this.rowHandles}
                    rowOperation = {this.rowOperation}
                    headerOperation = {this.headerOperation}
                    subtractHeight = '230'
                    subtractWidth ='20'
                    matchedRows ={this.props.matchedRows}
                    isRowSingle = "true"
                />
                <UpdateSerialrulePage
                    ref="updateSerialrule"
                    refreshHandle = {this.refreshHandle}
                />

                <EditPage
                    ref="editPage"
                    refreshHandle = {this.refreshHandle}
                />


            </div>
        )
    }
}
